package com.tomecode.tools.maven.settings.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.tomecode.tools.maven.settings.SettingsFactory;
import com.tomecode.tools.maven.settings.gui.tray.Tray;
import com.tomecode.tools.maven.settings.util.Backup;
import com.tomecode.util.gui.Frame;
import com.tomecode.util.gui.PanelFactory;
import com.tomecode.util.gui.component.Button;

/**
 * 
 * Restore settings.xml from backup
 * 
 * @author Frastia Tomas
 * 
 */
public final class FrmBackupList extends Frame {

	private static final long serialVersionUID = -3615241966947117203L;

	private final ListOfBackupsModel listOfBackupsModel;
	private final JList listOfBackups;

	private final Button bRestore;

	private final Button bChance;

	private FrmBackupList(final File settingsXml, List<Backup> backups) {
		super("Restore from Backup", 600, 300, true, false);

		addWindowListener(new WindowAdapter() {
			public final void windowClosing(WindowEvent winEvt) {
				Tray.getInstance().unblock();
			}
		});

		listOfBackupsModel = new ListOfBackupsModel(backups);
		listOfBackups = new JList(listOfBackupsModel);
		listOfBackups.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInList(listOfBackups.getSelectedIndex());
			}

		});

		listOfBackups.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInList(listOfBackups.getSelectedIndex());
			}
		});
		JPanel pList = PanelFactory.createBorderLayout(10, 10, 10, 10);
		pList.add(new JScrollPane(listOfBackups), BorderLayout.CENTER);
		addToContainer(pList, BorderLayout.CENTER);

		bRestore = new Button("Restore", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {

				File backup = listOfBackupsModel.get(listOfBackups.getSelectedIndex());
				try {
					SettingsFactory.restore(settingsXml, backup);
				} catch (IOException ex) {
					FrmError.showMe("Failed restore backup file '" + backup.getPath() + "'", ex);
				}
				setVisible(false);
				dispose();
				Tray.getInstance().reload();
			}
		});
		bChance = new Button("Cancel", new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
				Tray.getInstance().unblock();
			}
		});
		JPanel pButt = PanelFactory.createButtonsPanel(0, 10, 10, 10, bRestore, bChance);
		addToContainer(pButt, BorderLayout.SOUTH);
	}

	private final void selectInList(int selectedIndex) {
		if (selectedIndex == -1) {
			bRestore.setEnabled(false);
			return;
		}
		bRestore.setEnabled(true);
	}

	public static final void showMe(final File settingsXml, final List<Backup> backups) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new FrmBackupList(settingsXml, backups).setVisible(true);
			}
		});
	}

	/**
	 * 
	 * model for list of backups
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class ListOfBackupsModel extends AbstractListModel {

		private static final long serialVersionUID = 4861496510079422144L;

		private final List<Backup> backups;

		private ListOfBackupsModel(List<Backup> backups) {
			this.backups = backups;
		}

		private final File get(int selectedIndex) {
			return backups.get(selectedIndex).getFile();
		}

		public final Object getElementAt(int index) {
			return this.backups.get(index);
		}

		public final int getSize() {
			return this.backups.size();
		}

	}
}
